/*
 * CS_PK_ROOM_LIST.cpp
 *
 *  Created on: 2014. 1. 6.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../engine.h"
#include "../../User.h"
#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendAckFail(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_PK_ROOM_LIST);
	pUser->Send(pAck);
}

/*
// 배틀로얄 방리스트 요청
CS_PK_ROOM_LIST	0x141e
{
}

SC_PK_ROOM_LIST	0x141f
{
	s16  s16ack;
	u16  u16Count;	//방 개수(최대 99개)
	{
		u32 u32room_number;		//방번호
		u8  u8MaxMemeberCount;	//방인원 최대 수(배틀로얄은 4 혹은 3이 될 수 있음:우선은 3)
		u8  u8MemeberCount;		//해당 방의 대기인원 수
		{
			u8  u8class_type;	//대기자의 클래스
			u16 u16lv;			//대기자의 레벨
		}
	}
}
*/
void Command::CS_PK_ROOM_LIST(User * pUser, ::Network::Packet * pPacket)
{
	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAckFail(pUser, ACK_NO_SPACE);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAckFail(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAckFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	PREPARE_PACKET;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	auto & pkRoomTree = NLogic::Static::Instance().GETPKROOMTREE();

	NLogic::Static::PKROOMTREE pkRoomWaitTree;

	std::for_each(pkRoomTree.begin(), pkRoomTree.end(), [&](NLogic::Static::PKROOMTREE::value_type & pair){
		int levelGap = 0;
		if (pUser->GetCharacterInfo().level >= pair.second->GetMaster()->GetCharacterInfo().level)
			levelGap = pUser->GetCharacterInfo().level - pair.second->GetMaster()->GetCharacterInfo().level;
		else
			levelGap = pair.second->GetMaster()->GetCharacterInfo().level - pUser->GetCharacterInfo().level;

		if (10 > levelGap && NULL != pair.second->GetParty())
		{
			if (NLogic::Party::State::NEW_PK_ROOM_WAIT == pair.second->GetParty()->GetState())
				pkRoomWaitTree[pair.first] = pair.second;
		}
	});

	* pAck << static_cast<int16_t>(ACK_OK)
			<< static_cast<uint16_t>(pkRoomWaitTree.size());

	std::for_each(pkRoomWaitTree.begin(), pkRoomWaitTree.end(), [&](NLogic::Static::PKROOMTREE::value_type & pair){
		auto * pPKIndexer = pair.second->GetPKIndexer();

		* pAck	<< static_cast<uint32_t>(pPKIndexer->GetIndex())
				<< static_cast<uint8_t>(pair.second->GetData()._maxUser)
				<< static_cast<uint8_t>(pair.second->GetParty()->GetAllUserTree().size() );

		NLogic::Party::USERTREE & allUserTree = pair.second->GetParty()->GetAllUserTree();
		std::for_each(allUserTree.begin(), allUserTree.end(), [&](NLogic::Party::USERTREE::value_type & pair){
			* pAck	<< static_cast<uint8_t>(pair.second->GetCharacterInfo().classtype)
					<< static_cast<uint16_t>(pair.second->GetCharacterInfo().level);
		});
	});

	pAck->MakePacket(CMD_SC_PK_ROOM_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ }  /*ProjectZ*/
